package com.example.demo.controller;

import com.example.demo.VO.Result;
import com.example.demo.entity.Comment;
import com.example.demo.service.CommentService;
import com.example.demo.webconfig.Annotation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (Comment)表控制层
 *
 * @author hjl
 * @since 2020-11-30 19:33:01
 */
@RestController
@RequestMapping("comment")
@Api(value="用户controller",tags={"用户操作接口"})
public class CommentController {
    /**
     * 服务对象
     */
    @Resource
    private CommentService commentService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Comment selectOne(Integer id) {
        return this.commentService.queryById(id);
    }

    @Annotation.UserLoginToken
    @PostMapping("addCom")
    @ApiOperation(value="添加评论",tags={"comment"},notes="问题及用户不能为空")
    public Result addCom(Comment comment){
        if (comment.getQuestionId()==null || comment.getQuestionId().equals("")){
            return Result.failure("问题id不能为空");
        }
        if (comment.getQuestionType()==null || comment.getQuestionType().equals("")){
            return Result.failure("问题type不能为空");
        }
        if (comment.getUserId()==null || comment.getUserId().equals("")){
            return Result.failure("用户id不能为空");
        }
        if (comment.getUserComment()==null || comment.getUserComment().equals("")){
            return Result.failure("用户评论不能为空");
        }
        commentService.insert(comment);
        return Result.success(commentService.queryByExample(comment));
    }

    @Annotation.adminUserLoginToken
    @DeleteMapping("delCom")
    @ApiOperation(value="删除评论",tags={"comment"},notes="删除对象是否存在")
    public Result delCom(int id){
        Comment comment = commentService.queryById(id);
        if(comment==null){
            return Result.failure("删除对象不存在");
        }
        commentService.deleteById(id);
        return Result.success();
    }
    @ApiOperation(value="查看用户评论",tags={"comment"},notes="注意问题点")
    public Result selectByUserId(int userId){
        Comment comment = new Comment();
        comment.setUserId(userId);
        return Result.success(commentService.queryByExample(comment));
    }
    @ApiOperation(value="查看所有问题评论",tags={"questionId"},notes="注意问题点")
    public Result selectByQuestionId(int questionId){
        Comment comment = new Comment();
        comment.setQuestionId(questionId);
        return Result.success(commentService.queryByExample(comment));
    }

}